package worker;

import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.jms.JMSException;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import data.Data;

public class GetUrlDetail extends TimerTask{
	public void run() {
		DBCollection coll = Data.data("urlCraw");

		long time = new Date().getTime();
		Format formatter = new SimpleDateFormat("MM-dd-yy");
		
		String date = formatter.format(new Date());
		
		DBCursor lobj = coll.find(new BasicDBObject("status","true").append("active_craw", "yes"));
		
		DBCollection collection = Data.data("logclient");
		DBObject object = collection.findOne();
		object.put("log", new Date()+"|"+lobj.count());
		collection.save(object);
		
		DBCollection co = Data.data("users");
		
		while (lobj.hasNext()) {
			DBObject dbObject = lobj.next();
			
			boolean check = true;
			if(dbObject.get("update")==null||(dbObject.get("update")!=null&&!dbObject.get("update").toString().equals(date)))
			{
				dbObject.put("update", date);
				
				try {
					DBObject object2 = co.findOne(new BasicDBObject("username",dbObject.get("username")));
					if(object2!=null)
					{
						if(object2.get("status").toString().equals("trial")&&Long.parseLong(object2.get("timeend").toString())<time)
						{
							object2.put("coin", 0);
						} else {
							int coin = 0;
							if(object2.get("coin")!=null&&Integer.parseInt(object2.get("coin").toString())>0)
							{
								coin = Integer.parseInt(object2.get("coin").toString())-1;
							}
						
							try {
								if(object2.get("coin")!=null&&Integer.parseInt(object2.get("coin").toString())>0&&"yes".equals(dbObject.get("translate").toString()))
								{
									coin = Integer.parseInt(object2.get("coin").toString())-1;
								}
							} catch (Exception e) {
								// TODO: handle exception
							}
							try {
								if(object2.get("coin")!=null&&Integer.parseInt(object2.get("coin").toString())>0&&"yes".equals(dbObject.get("rewriter_title").toString()))
								{
									coin = Integer.parseInt(object2.get("coin").toString())-1;
								}
							} catch (Exception e) {
								// TODO: handle exception
							}
							object2.put("coin", coin);
						}
						co.save(object2);
						
						if(Integer.parseInt(object2.get("coin").toString())<=0)
						{
							check = false;
							dbObject.put("active_craw", "no");
						}
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				coll.save(dbObject);
			}
			if(check)
			{
				try {
					new GetUrl(dbObject);
				} catch (Exception e) {
					e.printStackTrace();
				}
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	public static void main(String[] args) throws JMSException, IOException {

		Timer timer = new Timer();
		GetUrlDetail setDataWorker = new GetUrlDetail();

	    // Schedule to run after every 3 second(3000 millisecond)

	    timer.scheduleAtFixedRate( setDataWorker, 0,1000*60*10);
	}
}
